package com.hongyi.excutService;

import java.util.concurrent.*;

/**
 * @Author: hongyi
 * @CreateTime: 2024-10-23
 */


public class Test {
    public static void main(String[] args) {
        // 定义一个固定大小的线程池
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        //  Executor threadPool = new ThreadPoolExecutor();
        String[] names = {"张三", "李四", "王五", "赵六", "钱七"};
        // 提交任务名称
        Future<String>[] futures = new Future[names.length];
        // 提交任务
        for (int i = 0; i < names.length; i++) {
            // 获取提交任务
            String name = names[i];
            // 调用 ApiTask 任务，并将返回的任务对象保存到 futures 数组中
            futures[i] = executorService.submit(new ApiTask(name));
        }

        // 获取提交任务
        for (int i = 0; i < futures.length; i++) {
            // 获取任务结果
            String result = null;
            try {
                result = futures[i].get();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            } catch (ExecutionException e) {
                throw new RuntimeException(e);
            }
            System.out.println(result);
        }

        // 关闭线程池
        executorService.shutdown();
    }

   /* public static class ApiTask implements Callable<String> {
        private final String name;

        public ApiTask(String name) {
            this.name = name;
        }


        @Override
        public String call() throws Exception {
            StringBuilder builder = new StringBuilder();

            for (int i = 1; i <= 10; i++) {
                builder.append(name).append("处理任务").append(":").append(i).append("\n");
            }
            return builder.toString();
        }
    }*/
}
